package org.scisaga.tetra.server;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Calendar;
import java.util.Properties;
import java.lang.management.ManagementFactory;
import org.json.JSONException;
import org.json.JSONStringer;

import com.sun.management.OperatingSystemMXBean;

public class SysTest {

	public static String test(String id){
		
		String prop = null;
		
		OperatingSystemMXBean osmb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
		Properties properties = System.getProperties();
		
		try {
			prop = new JSONStringer()
				    .object()
				    	.key("id").value(id)
				    	.key("host.ip").value(getHostIp())
				    	.key("host.name").value(getHostName())
				    	.key("host.time").value(Calendar.getInstance().getTime())
				    	//Memory & Processor Info
				    	.key("processor.available").value(osmb.getAvailableProcessors())
				    	.key("memory.total").value(osmb.getTotalPhysicalMemorySize())
				    	.key("memory.free").value(osmb.getFreePhysicalMemorySize())
				    	//Java
				    	.key("java.runtime.name").value(properties.getProperty("java.runtime.name"))
				    	.key("java.version").value(properties.getProperty("java.version"))
				    	//OS
				    	.key("os.name").value(properties.getProperty("os.name"))
				    	.key("os.version").value(properties.getProperty("os.version"))
				    	.key("user.name").value(properties.getProperty("user.name"))
				    	.key("user.language").value(properties.getProperty("user.language"))
				    	//Encoding
				    	.key("sun.jnu.encoding").value(properties.getProperty("sun.jnu.encoding"))
				    	.key("file.encoding").value(properties.getProperty("file.encoding"))
				    .endObject()
				.toString();
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
		return prop;
	}

	public static InetAddress getInetAddress(){

		try{
			return InetAddress.getLocalHost();
		}catch(UnknownHostException e){
			System.out.println("unknown host!");
		}
		return null;
	}

	public static String getHostIp(){
		InetAddress netAddress = getInetAddress();
		if(null == netAddress) return null;
		String ip = netAddress.getHostAddress(); //get the IP address
		return ip;
	}
	
	public static String getHostName(){
		InetAddress netAddress = getInetAddress();
		if(null == netAddress) return null;
		String name = netAddress.getHostName(); //get the host address
		return name;
	}
}